package com.justindav1s.homecontrol;

import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by justin on 10/11/2013.
 */
public class BluetoothInteractionManager extends Thread{

    private final BluetoothSocket socket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    private final Handler clientHandler;

    public BluetoothInteractionManager(BluetoothSocket _socket, Handler _clientHandler)  {

        socket = _socket;
        clientHandler = _clientHandler;

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            Message msg = new Message();
            msg.getData().putString(Constants.TYPE, Constants.DISCONNECTED_MSG_TYPE);
            msg.getData().putString(Constants.CONTENT, e.getMessage());
            clientHandler.sendMessage(msg);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;

    }

    public void run() {
        byte[] buffer = new byte[7];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                Message msg = new Message();
                msg.getData().putString(Constants.TYPE, Constants.DATA_MSG_TYPE);
                msg.getData().putString(Constants.CONTENT, new String(buffer));
                clientHandler.sendMessage(msg);
            } catch (IOException e) {
                Message msg = new Message();
                msg.getData().putString(Constants.TYPE, Constants.DISCONNECTED_MSG_TYPE);
                msg.getData().putString(Constants.CONTENT, e.getMessage());
                clientHandler.sendMessage(msg);
                break;
            }
        }

    }

    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) {
            Message msg = new Message();
            msg.getData().putString(Constants.TYPE, Constants.DISCONNECTED_MSG_TYPE);
            msg.getData().putString(Constants.CONTENT, e.getMessage());
            clientHandler.sendMessage(msg);
        }
    }

    /* Call this from the main activity to shutdown the connection */
    public void close() {
        try {
            socket.close();
        } catch (IOException e) {
            Message msg = new Message();
            msg.getData().putString(Constants.TYPE, Constants.DISCONNECTED_MSG_TYPE);
            msg.getData().putString(Constants.CONTENT, e.getMessage());
            clientHandler.sendMessage(msg);
        }
    }
}
